﻿using System;
using System.Data;
using System.Data.SqlClient;

//public interface DataBaseOperate_Interface {
    
//    DataTable GetDataTable(string sqlstr);
    
//    DataRow GetDataRow(string sqlstr);
    
//    bool GetYesOrNo(string sqlstr);
    
//    object GetValue(string sqlstr);
    
//    bool ExecUpdate(string sqlstr);

//}
    
    public class DataBaseOperate_SQLServer {
        
        public static SqlDataAdapter objDataAdpter;
        
        public static SqlConnection objConnection;
        
        public const string DataConnStr = "Data Source=localhost;Initial Catalog=holtmann_henghoui;Integrated Security=True;packet size=4096;Pooling=true;Min Pool Size=1;Max Pool Size=8;";
        
        private static void DataConnInit() {
            if ((objConnection == null)) {
                objConnection = new System.Data.SqlClient.SqlConnection(DataConnStr);
                // Warning!!! Optional parameters not supported
            }
        }
        
        public static DataTable GetDataTable(string sqlstr) {
            DataConnInit();
            DataTable dt = new DataTable("t1");
             try {
                 if ((objConnection.State == ConnectionState.Closed))
                 {
                     objConnection.Open();
                 }
                objDataAdpter = new SqlDataAdapter(sqlstr, objConnection);
                objDataAdpter.Fill(dt);
                return dt;
            }
            catch (Exception ex) {
                Log.Record(ex, ("产生错误的SQL语句:" + sqlstr));
                return null;
            }
        }
        
        public static DataRow GetDataRow(string sqlstr) {
            DataTable dt = GetDataTable(sqlstr);
            if ((!(dt == null) 
                        && (dt.Rows.Count > 0))) {
                return dt.Rows[0];
            }
            else {
                return null;
            }
        }
        
        public static bool GetYesOrNo(string sqlstr) {
            if ((GetValue(sqlstr) == null)) {
                return false;
            }
            else {
                return true;
            }
        }
        
        public static object GetValue(string sqlstr) {
            DataConnInit();
            object Result = null;
            try {
                if ((objConnection.State == ConnectionState.Closed)) {
                    objConnection.Open();
                }
                SqlCommand objCommand = new SqlCommand(sqlstr, objConnection);
                Result = objCommand.ExecuteScalar();
            }
            catch (System.Exception ex) { 
                Log.Record(ex, ("产生错误的SQL语句:" + sqlstr));
            }
            return Result;
        }

        
        public static bool ExecUpdate(string sqlstr) {
            DataConnInit();
            try {
                if ((objConnection.State == ConnectionState.Closed)) {
                    objConnection.Open();
                }
                SqlCommand objCommand = new SqlCommand(sqlstr, objConnection);
                objCommand.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex) { 
                Log.Record(ex, ("产生错误的SQL语句:" + sqlstr));
                return false;
            }
        }


        public static bool ExecUpdate(SqlCommand objCommand)
        {
            DataConnInit();
            try
            {
                if ((objConnection.State == ConnectionState.Closed))
                {
                    objConnection.Open();
                }
                objCommand.Connection = objConnection;
                objCommand.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                Log.Record(ex);
                return false;
            }
        }

    }

